package com.winning.bi.etl;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

import javax.sql.DataSource;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.winning.bi.jdbc.dao.Dao;

/**
 * 
 * @author 张大川
 *
 */
public class TargetList extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private DataSource dataSource;
	String tableName;

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	String[][] rowData;

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * Create the frame.
	 * 
	 * @throws SQLException
	 */
	public TargetList(ClassPathXmlApplicationContext ac, String dbname,String daoString) {
		super("目标数据表");
		// setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (StringUtils.isBlank(tableName)) {
					 int exi = JOptionPane.showConfirmDialog (contentPane, "没有选择数据表确定要退出吗？", "友情提示", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
					if(exi==JOptionPane.YES_OPTION)
					{
						dispose();
					}
					else
					{
						//this.f
						return ;
					}
					
				}
				else {
					dispose();
				}

			}

		});
		setBounds(100, 100, 250, 600);
		contentPane = new JPanel();
		contentPane.setPreferredSize(new Dimension(200, 500));//
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		JPanel jPanel = new JPanel();
		JPanel jPanel2 = new JPanel();

		JLabel jLabel = new JLabel("数据表");
		jPanel.add(jLabel);
		JScrollPane jScrollPane1 = new JScrollPane();
		JButton button = new JButton("完成设置");

		JButton button2 = new JButton("取消");
		button2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				dispose();
			}
		});
		JToolBar toolBar = new JToolBar();
		toolBar.add(button);
		toolBar.addSeparator();
		toolBar.add(button2);
		toolBar.setFloatable(true);
		jPanel2.add(toolBar);

		final Object[] columnNames = { "表名", "注释" };
		DataSource dataSource = (DataSource) ac.getBean(dbname);
		Dao dao = (Dao) ac.getBean(daoString);
		try {
			rowData = dao.getTableNames(dataSource, "dbo");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// setTableName("xxx");
		JTable jtable1 = new JTable(rowData, columnNames);
		// 设置数据
		jScrollPane1.setViewportView(jtable1); // 不能直接add
		jScrollPane1.setPreferredSize(new Dimension(200, 200));
		contentPane.add(jPanel, BorderLayout.NORTH);
		contentPane.add(jScrollPane1, BorderLayout.CENTER);
		contentPane.add(jPanel2, BorderLayout.SOUTH);
		setContentPane(contentPane);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int i = jtable1.getSelectedRow();
				if (i > -1) {
					String[] strings = rowData[i];
					// System.out.println(strings[0]);
					setTableName(strings[0]);
					JOptionPane.showMessageDialog(contentPane, "选中" + strings[0] + "表", "提示",
							JOptionPane.PLAIN_MESSAGE);
					return;

				} else {
					JOptionPane.showMessageDialog(contentPane, "没有选择数据", "提示", JOptionPane.WARNING_MESSAGE);
					return;
				}
			}

		});
	}

}
